package edu.kaist.cs408.pof.android;

import android.content.Context;
import android.widget.SimpleExpandableListAdapter;
import edu.kaist.cs408.pof.common.model.DishVo;
import edu.kaist.cs408.pof.common.model.RestaurantVo;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: duyleekun
 * Date: 5/15/12
 * Time: 12:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class RestaurantListAdapter extends SimpleExpandableListAdapter {

	private RestaurantListAdapter(Context context, List<? extends Map<String, ?>> groupData, int groupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom, int[] childTo) {
		super(context, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo);
	}

	private RestaurantListAdapter(Context context, List<? extends Map<String, ?>> groupData, int expandedGroupLayout, int collapsedGroupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom, int[] childTo) {
		super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo);
	}

	private RestaurantListAdapter(Context context, List<? extends Map<String, ?>> groupData, int expandedGroupLayout, int collapsedGroupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData, int childLayout, int lastChildLayout, String[] childFrom, int[] childTo) {
		super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom, groupTo, childData, childLayout, lastChildLayout, childFrom, childTo);
	}

	public static RestaurantListAdapter getInstance(Context context, List<RestaurantVo> restaurants) {
		// 1D Array x 1D Map
		LinkedList<Map<String, Object>> groupData = new LinkedList<Map<String, Object>>();

		// 2D Array x 1D Map
		LinkedList<LinkedList<HashMap<String, Object>>> childData = new LinkedList<LinkedList<HashMap<String, Object>>>();

		final String groupKey = "restaurantName";
		final String childKey = "dishName";
		final String objectKey = "object";

		for (RestaurantVo restaurant : restaurants) {

			HashMap<String, Object> currentGroup = new HashMap<String, Object>();


			currentGroup.put(groupKey, restaurant.getName());
			currentGroup.put(objectKey, restaurant);
			groupData.add(currentGroup);


			LinkedList<HashMap<String, Object>> currentGroupChild = new LinkedList<HashMap<String, Object>>();
			childData.add(currentGroupChild);
			for (DishVo dish : restaurant.getDishes()) {
				HashMap<String, Object> currentChildChild = new HashMap<String, Object>();
				currentChildChild.put(childKey, dish.getName());
				currentChildChild.put(objectKey, dish);
				currentGroupChild.add(currentChildChild);
			}
		}

		return new RestaurantListAdapter(context, groupData, R.layout.child_row, new String[]{groupKey}, new int[]{R.id.childName}, childData, R.layout.child_row, new String[]{childKey}, new int[]{R.id.wordsContent});

	}


}